<script type="text/javascript" charset="utf-8">

    var dlgLogin,dlgChangePassword;
    var fmLogin,fmChangePassword;

    $(function(){

        fmLogin=$("#fmLogin").form({
            url:'{{ url_for('login') }}',
            success:function(r){
                console.log(r);
                r=$.parseJSON(r);
                console.log(r);
                if(r && r.bSuccess){
                    dlgLogin.dialog('close');
                    location.href='{{ url_for('index') }}'
                }else{
                    $.messager.confirm('错误', r.sMsg, function(res){
                        //定位到登录表单的用户名输入框并且选中内容
                        if(r.iErr==300){
                            fmLogin.find('input').last().select();
                        }
                        if(r.iErr==400){
                            fmLogin.find('input').first().select();
                        }

                    });
                }
            }
        });

        fmChangePassword=$("#fmChangePassword").form({
            url:'{{ url_for('changepassword') }}',
            success:function(r){
                console.log(r);
                r=$.parseJSON(r);
                console.log(r);
                if(r && r.bSuccess){
                    dlgChangePassword.dialog('close');
                    //location.href='{{ url_for('index') }}'
                    $.messager.show({
                        title:'成功',
                        msg: r.sMsg,
                        showType:'show'
			        });
                }else{
                    $.messager.confirm('错误', r.sMsg, function(res){
                        //定位到登录表单的用户名输入框并且选中内容
                        if(r.iErr==301){
                            fmChangePassword.find('input').eq(1).select();
                        }
                        if(r.iErr==302){
                            fmChangePassword.find('input').first().select();
                        }
                        if(r.iErr==400){
                            location.href='{{ url_for('logout') }}';
                        }

                    });
                }
            }
        });

        dlgLogin=$("#dlgLogin").show().dialog({
            modal: true,
            closable: true,
            closed:true,
            buttons: [{
                text:'登录',
                iconCls:'icon-ok',
                handler:function(){
                    fmLogin.submit();
                }
            },{
                text:'取消',
                handler:function(){
                    dlgLogin.dialog('close');
                }
            }]
        });

        dlgChangePassword=$("#dlgChangePassword").show().dialog({
            modal: true,
            closable: true,
            closed:true,
            buttons: [{
                text:'确定',
                iconCls:'icon-ok',
                handler:function(){
                    fmChangePassword.submit();
                }
            },{
                text:'取消',
                handler:function(){
                    dlgChangePassword.dialog('close');
                }
            }]
        });


        $("#aLogin").click(function(){
            //清空登录表单
{#            fmLogin[0].reset();#}
            fmLogin.form('clear');
            dlgLogin.dialog('open');
            //登录表单的用户名输入框获得焦点
            fmLogin.find('input').first().focus();
        });

        $("#aLogout").click(function () {
            $.messager.confirm('退出系统','确定要退出系统?',function(res){
                if(res){
                    location.href='{{ url_for('logout') }}';
                }
            })
        });

        $("#aChangePassword").click(function(){
            //清空修改密码表单
{#            fmChangePassword[0].reset();#}
            fmChangePassword.form('clear');
            dlgChangePassword.dialog('open');
            //修改密码表单的原密码输入框获得焦点
            fmChangePassword.find('input').first().focus();
        });

        $("#aLogout").click(function () {
            $.messager.confirm('退出系统','确定要退出系统?',function(res){
                if(res){
                    location.href='{{ url_for('logout') }}';
                }
            })
        });


        //登录界面支持回车提交，ESC键关闭对话框
        fmLogin.keypress(function(event){
            code = event.keyCode || event.which
            console.log(code);
            if(code==13){
                $(this).submit();
            }
        }).keyup(function(event){
            code = event.keyCode || event.which
            console.log(code);
            if(code==27){
                dlgLogin.dialog('close');
            }
        });

        //修改密码界面支持回车提交，ESC键关闭对话框
        fmChangePassword.keypress(function(event){
            code = event.keyCode || event.which
            console.log(code);
            if(code==13){
                $(this).submit();
            }
        }).keyup(function(event){
            code = event.keyCode || event.which
            console.log(code);
            if(code==27){
                dlgChangePassword.dialog('close');
            }
        });

    });
</script>
<div id="dlgLogin" title="登录界面" style="display:none;width:240px;height:200px;padding:10px">
    <form id="fmLogin" method="post">
        <div style="padding:5px 30px;">
            <table>
                <tr>
                    <td>
                        <label for="uuser">用户名*</label>
                        <input id="uuser" class="easyui-validatebox" name="uuser" type="text" data-options="required:true" style="width: 141px; height: 15px">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="upass">密&nbsp;&nbsp;码*</label>
                        <input id="upass" class="easyui-validatebox" name="upass" type="password" data-options="required:true,validType:'pass'" style="width: 141px; height: 15px">
                    </td>
                </tr>
            </table>
        </div>
    </form>
</div>
<div id="dlgChangePassword" title="修改密码" style="display:none;width:240px;height:260px;padding:10px">
    <form id="fmChangePassword" method="post">
        <div style="padding:5px 30px;">
            <table>
                <tr>
                    <td>
                        <label for="uuser">{{ session['user'] }}</label>

                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="uopass">原&nbsp;密&nbsp;码*</label>
                        <input id="uopass" class="easyui-validatebox" name="uopass" type="password" data-options="required:true,validType:'pass'" style="width: 141px; height: 15px">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="unpass">新&nbsp;密&nbsp;码*</label>
                        <input id="unpass" class="easyui-validatebox" name="unpass" type="password" data-options="required:true,validType:'pass'" style="width: 141px; height: 15px">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="uqpass">重复密码*</label>
                        <input id="uqpass" class="easyui-validatebox" name="uqpass" type="password" data-options="required:true,validType:'pass'" style="width: 141px; height: 15px">
                    </td>
                </tr>
            </table>
        </div>
    </form>
</div>
<div id="mm" class="easyui-menu" style="display:none;width:150px;">
    <div id="mm-refresh" data-options="iconCls:'icon-reload'">刷新</div>
    <div class="menu-sep"></div>
    <div id="mm-closeall" data-options="iconCls:'icon-no'">关闭全部标签页</div>
    <div id="mm-closeother" data-options="iconCls:'icon-cut'">关闭其他标签页</div>
    <div class="menu-sep"></div>
    <div id="mm-close" data-options="iconCls:'icon-remove'">关闭标签页</div>
</div>
<div id="northPanel" data-options="region:'north', border:false" style="height:60px; padding: 5px; background-color:#E0ECFF; overflow: hidden;">
    <div id="topbar" class="top-bar">
        <div class="top-bar-left">
            <h2 style="margin-left: 10px; margin-top: 10px;">EasyUI</h2>
        </div>
        <div class="top-bar-right">
            <div id="timerSpan"></div>
            <div id="dvLoginBar">
                {% if session['user'] %}
                <a href="#" id="aChangePassword">{{ session['name'] }}</a>
                    |
                <a href="#" id="aLogout">退出</a>
                {% else %}
                <a href="#" id="aLogin">登录</a>
                {% endif %}
            </div>
        </div>
    </div>
</div>
